<template>
  <div class="login-container">
    <!--
  size:用于控制该表单内组件的尺寸大小 有三个值 medium / small / mini
  rules:表单验证规则
  ref：类型于平时的 div 给定的 id
  model: 绑定数据
    -->
    <el-form
      ref="loginForm"
      size="medium"
      :rules="rules"
      :model="loginForm"
      class="loginForm"
    >
      <el-form-item label>
        <div class="login-title">小区物业系统</div>
      </el-form-item>
      <el-form-item prop="username" label>
        <el-input
          v-model="loginForm.username"
          type="text"
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item prop="password" label>
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item prop="userType">
        <el-radio-group v-model="loginForm.userType">
          <el-radio :label="0">业主</el-radio>
          <el-radio :label="1">物业</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-button class="my-button" type="primary" @click="submit()">登录</el-button>
          </el-col>
          <el-col :span="12">
            <el-button class="my-button">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {

      imgSrc: '',

      // 登陆表单数据
      loginForm: {
        username: '', // 登录名
        password: '', // 密码
        userType: '' // 用户类型 0：业主 1：物业
      },

      // 登陆表单校验
      rules: {
        username: [
          {
            required: true,
            trigger: 'change',
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'change',
            message: '请输入密码'
          }
        ],
        userType: [
          {
            required: true,
            trigger: 'change',
            message: '请选择用户类型'
          }
        ]
      }
    }
  },
  methods: {
    async submit() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../../assets/images/login_bg.png");
}
.loginForm{
  height: 350px;
  width: 450px;
  border-radius: 10px;
  box-shadow: 0 0 25px #cac6c6;
  padding: 20px 35px;
  background-color: #fff;
}
.login-title {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
}
.my-button {
  width: 100%;
}
</style>
